package com.jincp.image.pick.ui.video;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.jcp.baselibrary.model.ToolBarOptions;
import com.jincp.image.pick.FileBaseActivity;
import com.jincp.image.pick.ImagePicker;
import com.jincp.image.R;
import com.jincp.image.pick.VideoPicker;
import com.jincp.image.pick.bean.VideoItem;

public class PickVideoActivity extends FileBaseActivity {

    public static final int REQUEST_CODE = 0x00300;

    public static final int FROM_LOCAL = 1;

    public static final int FROM_CAMERA = 2;

    private VideoPicker videoPicker;

    public static void start(Activity activity, int requestCode, int from) {
        Intent intent = new Intent(activity, PickVideoActivity.class);
        intent.putExtra("EXTRA_FROM", from);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void start(Fragment fragment, int requestCode, int from) {
        Intent intent = new Intent(fragment.getActivity(), PickVideoActivity.class);
        intent.putExtra("EXTRA_FROM", from);
        fragment.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_image);
        setToolBar(R.id.toolbar, new ToolBarOptions("选择视频"));
        videoPicker = VideoPicker.getInstance();
        processIntent();
    }

    private void processIntent() {
        int from = getIntent().getIntExtra("EXTRA_FROM", FROM_LOCAL);
        if (from == FROM_LOCAL) {
            pickFromLocal();
        } else if (from == FROM_CAMERA) {
            pickFromCamera();
        }
    }

    private void pickFromLocal() {
        Intent intent = new Intent(this, VideoGridActivity.class);
        startActivityForResult(intent, 100);
    }

    private void pickFromCamera() {
        try {
            videoPicker.takeRecord(this, VideoPicker.REQUEST_VIDEO_TAKE);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            finish();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, R.string.sdcard_not_enough_head_error, Toast.LENGTH_LONG).show();
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CANCELED) {
            finish();
        }
        if (resultCode == RESULT_OK && requestCode == VideoPicker.REQUEST_VIDEO_TAKE){
                //发送广播通知视频增加了
                VideoPicker.galleryAddPic(this, videoPicker.getTakeVideoFile());
                VideoItem videoItem = new VideoItem();
                videoItem.path = videoPicker.getTakeVideoFile().getAbsolutePath();
                videoPicker.clearSelectedVideos();
                //添加选中这个录像视频
                videoPicker.addSelectedVideoItem(0, videoItem, true);
                //更新界面设置录像的这个视频为选中状态  在Adapter中使用接口实现
                Intent intent = new Intent();
                intent.putExtra(VideoPicker.EXTRA_RESULT_VIDEO_ITEMS, videoPicker.getSelectedVideos());
                setResult(RESULT_OK, intent);
                finish();
        }
        if (resultCode == VideoPicker.RESULT_VIDEO_ITEMS) {
            if (data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS) == null) {
                //什么都不做
                finish();
            } else {
                //说明是从裁剪页面过来的数据，直接返回就可以
                setResult(RESULT_OK, data);
                finish();
            }
        }
    }
}
